// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/visitor/api/MeetingLocationControllerApi'

import TreeClass from '@api/visitor/model/MeetingLocationListReq'
import OneClass from '@api/visitor/model/MeetingLocationDetailReq'
import AddClass from '@api/visitor/model/InsertMeetingLocationReq'
import EditClass from '@api/visitor/model/UpdateMeetingLocationReq'
import DelClass from '@api/visitor/model/DeleteMeetingLocationReq'
import BaseClass from '@api/visitor/model/MeetingLocationDetailDto'
import ChildClass from '@api/visitor/model/MeetingPermissionDto'

const CLIENT = CORE.grpcCLIENT(Client)
const CHILDREN = [ChildClass, 'meetingPermissions', 1]
const BASE = [BaseClass, 'content', null, (dto, obj) => CORE.grpcDTO(dto, obj, CHILDREN)]

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 结构树 —————————————————————————————— //

  async getTree(treeParam, TIP = '查询结构树') {
    const DTO = CORE.grpcDTO(TreeClass, treeParam)
    const resp = await CORE.grpc(TIP, CLIENT, 'meetingLocationListAdmin', DTO)
    return resp.content || []
  },
  transTree(arr) {
    arr.forEach((item) => {
      item.id = item.id || item.meetingLocationId
    })
    return arr
  },

  // —————————————————————————————— 单项 —————————————————————————————— //

  async getOne({ id }, TIP = '查询单项') {
    const DTO = CORE.grpcDTO(OneClass, { id })
    const resp = await CORE.grpc(TIP, CLIENT, 'meetingLocationDetail', DTO)
    return resp.content || {}
  },
  transOne(obj) {
    if (obj.meetingPermissionType === 1) {
      const auth = JSON.parse(obj.meetingPermission || '{}')
      Object.assign(obj, { authId: auth.id, authName: auth.name })
    } else {
      obj.authList = obj.meetingPermissions.map((item) => {
        const auth = JSON.parse(item.meetingPermission || '{}')
        const organList = JSON.parse(item.organInfos || '[]')
        obj[`child_${item.id}-organ`] = obj[`child_${item.id}-auth`] = 'ready'
        return {
          authId: auth.id,
          authName: auth.name,
          organIdList: organList.map((item) => item.id),
          organNameList: organList.map((item) => item.name),
        }
      })
    }
    return obj
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one, [BaseClass, 'content'])
    await CORE.grpc(TIP, CLIENT, 'insertMeetingLocation', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  transEdit(one) {
    if (one.meetingPermissionType === 1) {
      one.meetingPermission = JSON.stringify({ id: one.authId, name: one.authName })
      one.meetingPermissions = []
    } else {
      one.meetingPermission = JSON.stringify({})
      one.meetingPermissions = one.authList.map((item) => {
        const organList = item.organIdList.map((id, i) => ({ id, name: item.organNameList[i] }))
        const auth = { id: item.authId, name: item.authName }
        return { meetingPermission: JSON.stringify(auth), organInfos: JSON.stringify(organList) }
      })
    }
    return one
  },
  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one, BASE)
    await CORE.grpc(TIP, CLIENT, 'updateMeetingLocation', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { id })
    await CORE.grpc(TIP, CLIENT, 'deleteMeetingLocation', DTO)
    return CORE.grpcOK(TIP)
  },
}
